﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JEng_Mark_1.SceneObject;
using JEng_Mark_1.Shaders;
using JEng_Mark_1.Managers;
using JEng_Mark_1.Interfaces;
using JEng_Mark_1.Models;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using JEng_Mark_1.Textures;

namespace JEng_TechDemo
{
    public class Teapot : JSceneObject, IJEngLoadable
    {
        private JTexture _diff, _norm;
        public Teapot()
        {
            lizard_xnaEffect effect = new lizard_xnaEffect();

            JShaderManager.AddShader(effect, "test_shader");
            Material = new JMaterial();
            Material.Shader = "test_shader";
        }

        public void  LoadContent()
        {
            JModel model = new JModel("Content/Models/room_basic");
            JModelManager.AddModel(model, "teapot");
            this.ModelName = "teapot";
            this.Scale = new Vector3(100f);
            model.LoadContent();

            //override shader and use custom shader
            model.SetEffect(JShaderManager.GetShader("test_shader").BaseEffect);
            _diff = new JTexture("Content/Models/Lizard/lizard_diff");
            _norm = new JTexture("Content/Models/Lizard/lizard_norm");

            JTextureManager.AddTexture(_diff, "lizard_diffuse");
            JTextureManager.AddTexture(_norm, "lizard_normal");

            ReadyToRender = true;
 	        
        }

        public void  UnloadContent()
        {
 	        
        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if (ReadyToRender)
            {
                lizard_xnaEffect shader = JShaderManager.GetShader("test_shader") as lizard_xnaEffect;

                //shader.Wvp = 
                IJEngModel model = JModelManager.GetModel(ModelName);
                if (model != null && model.ReadyToRender)
                {
                    Matrix[] transforms = new Matrix[model.BaseModel.Bones.Count];
                    model.BaseModel.CopyAbsoluteBoneTransformsTo(transforms);



                    foreach (ModelMesh mesh in model.BaseModel.Meshes)
                    {
                        foreach (Effect effect in mesh.Effects)
                        {
                            
                            Matrix wvp = Matrix.Multiply(World, Matrix.Multiply(JCameraManager.ActiveCamera.View, JCameraManager.ActiveCamera.Projection));
                            effect.Begin();
                            effect.Parameters["world"].SetValue(World);
                            effect.Parameters["wvp"].SetValue(World * JCameraManager.ActiveCamera.View * JCameraManager.ActiveCamera.Projection);
                            //effect.Parameters["projection"].SetValue(JCameraManager.ActiveCamera.Projection);
                            effect.Parameters["TextureMap_1392"].SetValue(_diff.BaseTexture);
                            effect.Parameters["NormalMap_5989"].SetValue(_norm.BaseTexture);
                            //effect.Parameters["LightPosition"].SetValue(new Vector3(0, 20, 0));
                            //effect.Parameters["LightColor"].SetValue(new Vector4(1f, 1f, 1f,1.0f));
                            //effect.Parameters["AmbientLightColor"].SetValue(new Vector4(1f, 1f, 1f,1.0f));
                            //effect.Parameters["Shininess"].SetValue(1.0f);
                            //effect.Parameters["SpecularPower"].SetValue(100.0f);

                            //effect.Parameters["wvp"].SetValue(wvp);
                            effect.Parameters["worldIT"].SetValue(Matrix.Invert(Matrix.Transpose(World)));
                            effect.Parameters["worldI"].SetValue(Matrix.Invert(World));
                            //effect.Parameters["world"].SetValue(World);
                            effect.Parameters["viewInv"].SetValue(Matrix.Invert(JCameraManager.ActiveCamera.View));
                            effect.End();
                            /*effect.Parameters["UIColor_8925"].SetValue(shader.UIColor_8925);
                            effect.Parameters["light1Pos"].SetValue(shader.Light1Pos);
                            effect.Parameters["light1Hotspot"].SetValue(shader.Light1Hotspot);
                            effect.Parameters["light1Falloff"].SetValue(shader.Light1Falloff);
                            effect.Parameters["light1Dir"].SetValue(shader.Light1Dir);
                            effect.Parameters["light1Color"].SetValue(shader.Light1Color);
                            effect.Parameters["light1Attenuation"].SetValue(shader.Light1Attenuation);*/
                            //effect.End();
                        }
                        mesh.Draw();
                    }
                }
            }
        }

}
}
